﻿@using winproject.Models;
@{
    ViewBag.Title = "Lists";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";

    int resCode = 0;
    ProductDetails oItemEditDetail = null;
    if (ViewData != null && ViewData.Keys.Count() > 0 && ViewData.Keys.Contains("resCode"))
    {
        resCode = Convert.ToInt32(ViewData["resCode"]);
    }
    if (resCode == 250)
    {
        oItemEditDetail = (ProductDetails)ViewData["resData"];
    }
}

@Styles.Render("~/Content/Vendors")
@Html.Partial("_LeftMenu")


<!-- content -->
<div class="col-md-10">
    
    <div class="row">
        <div class="panel panel-default bootstrap-admin-no-table-panel">
            <div class="panel-heading">
                <div class="text-muted bootstrap-admin-box-title">Product > Details</div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="panel panel-default bootstrap-admin-no-table-panel">
            <div class="panel-heading">
                <div class="text-muted bootstrap-admin-box-title">Filter your listings</div>
                    <form id="frmProduct" action="~/Products/Lists"  method="post" enctype="multipart/form-data" >
                    @{
                        if (oItemEditDetail != null && (oItemEditDetail.nPid) > 0)
                        {
                            <input type="hidden" id="txtUniqueId" name="txtUniqueId" value="@oItemEditDetail.nPid" />
                        }
                          else{
                            <input type="hidden" id="txtUniqueId" name="txtUniqueId" value="0" />   
                        }
                     }
                        <table cellpadding="5" cellspacing="5">
                    <tr>
                        <td>
                            Select Category
                        </td>
                        <td>
                            <select id="drpCategory" name="drpCategory" onchange="return getBasedSubCategory(this.value,'drpSubCategory',containerTypes.options);">
                                <option value="0">--Select Category--</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Select Sub Category
                        </td>
                        <td>
                            <select id="drpSubCategory" name="drpSubCategory">
                                <option value="0">--Select Sub Category--</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Enter Product Name:
                        </td>
                        <td>
                            <input type="text" id="txtSearch" name="txtSearch" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            &nbsp;
                        </td>
                        <td>
                            <button type="button" value="Find Products" onclick="return findSpecificProducts();">Search</button>
                            &nbsp;&nbsp;
                            <button type="button" value="Reset" onclick="window.location.href='@HttpContext.Current.Request.ApplicationPath/Products/Lists'">Cancel</button>
                        </td>
                    </tr>
                </table>
                    </form>
            </div>
            
            <div class="bootstrap-admin-no-table-panel-content bootstrap-admin-panel-content collapse in">
                <div class="row">
                    <div class="col-md-12">
                        <table width="100%" cellpadding="5" cellspacing="0" class="table-striped">
                            <thead>
                                <th>Sr.No</th>
                                <th>Title</th>
                                <th>Category</th>
                                <th>Subcategory</th>
                                <th>Download</th>
                                <th>Buy Now</th>
                                <th>Company</th>
                                <th>BasePrice</th>
                                <th>Discount</th>
                                <th>NetPrice</th>
                                <th>Action</th>
                            </thead>
                            <tbody id="list_info">
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>

<script type="text/html" id="tpl_listinfo">
    <tr id="info-{itemid}-item">
        <td>
            {srno}
        </td>
        <td>
            <a title="{pname} Slug: {pslug}" alt="{pname}" target="_blank" href="{phlnk}">{pname}</a>
        </td>
        <td>
            <a title="{pcname}" alt="{pcname}" target="_blank" href="javascript:void(0);">{pcname}</a>
        </td>
        <td>
            <a title="{pscname}" alt="{pscname}" target="_blank" href="javascript:void(0);">{pscname}</a>
        </td>
        <td>
            <a title="{pdlnk}" alt="{pdlnk}" target="_blank" href="{pdlnk}">Download</a>
        </td>
        <td>
            <a title="{pblnk}" alt="{pblnk}" target="_blank" href="{pblnk}">Buy</a>
        </td>
        <td>
            <a title="{cname}" alt="{cname}" target="_blank" href="{phlnk}">{cname}</a>
        </td>
        <td>
            {cur}{bprice}
        </td>
        <td>
            {disc}%
        </td>
        <td>
            {cur}{nprice}
        </td>
        <td>
            <a href="javascript:void(0)"  onclick="return editItem({itemid});">Edit</a>
            &nbsp;/&nbsp; 
            <a href="javascript:void(0)"  onclick="return editProperty({itemid});">Property</a>
            &nbsp;/&nbsp; 
            <a href="javascript:void(0)"  onclick="return editStats({itemid});">Stats</a>
            &nbsp;/&nbsp; 
            <a href="javascript:void(0)" class="del-action"  data-toggle="modal" data-target="#myModal"  onclick="return deleteItem({itemid},'proddetail-item');">Delete</a>
        </td>
    </tr>
</script>



<script>
    var oMycontentLoader = new MyContentLoader();

    //Declarations
    var itemGridListGlobal = [];



    function editItem(itemid) {
        if (!isNaN(itemid) && parseInt(itemid) > 0) {
            $("#txtUniqueId").val(itemid);
            $("#frmProduct").attr("action", approotpath + "Products/Details");
            $("#frmProduct").submit();
        }
    }

    function editProperty(itemid) {
        if (!isNaN(itemid) && parseInt(itemid) > 0) {
            $("#txtUniqueId").val(itemid);
            $("#frmProduct").attr("action", approotpath + "Products/ProductProperties");
            $("#frmProduct").submit();
        }
    }
    function editStats(itemid) {
        if (!isNaN(itemid) && parseInt(itemid) > 0) {
            $("#txtUniqueId").val(itemid);
            $("#frmProduct").attr("action", approotpath + "Products/ProductStatus");
            $("#frmProduct").submit();
        }
    }

    function deleteItem(itemid, targetitem) {
        if (confirm("Are you sure want to delete?")) {
            switch (targetitem) {
                case 'proddetail-item':
                    deleteProdDetailItem(itemid);
                    break;
            }
        }
    }
    function deleteProdDetailItem(itemid) {
        if (!isNaN(itemid) && parseInt(itemid) > 0) {
            var oItemdetail = new ProductDetails();
            oItemdetail.nPid = itemid;
            $(document).ready(function () {
                $.ajax(approotpath + "Products/DeleteProduct", {
                    data: oItemdetail,
                    type: "POST",
                    dataType: 'json',
                    success: function (data) {
                        var dataJson = data;//$.parseJSON(data);
                        if (parseInt(dataJson.errCode) == 250) {
                            if (document.getElementById("info-" + itemid + "-item")) {
                                $("#info-" + itemid + "-item").remove();
                                if ($("#list_info").find("tr").length <= 0) {
                                    $("#list_info").append(oMycontentLoader.noRecordExist(12, "No More Records Exists"));
                                }
                                alert("Item removed");
                            }
                        }
                    }
                });
            });
        }
    }

    //get virus detail list on load time
    $(document).ready(function () {
        //load category
        getBasedCategory('drpCategory', containerTypes.options);

        oMycontentLoader.showLoader("list_info", 12, true);
        $.ajax(approotpath + "Helper/GetProductDetailsJSON", {
            data: {},
            type: "GET",
            dataType: 'json',
            success: function (data) {
                var dataJson = $.parseJSON(data.msgJson);
                makeItemGrid(dataJson);
                //self.categories(mappedCategories);
            },
            error: function (jqXHR, status, err) {
                console.log("Error raised: " + err);
                $("#list_info").append(oMycontentLoader.noRecordExist(12, "Oop's Something went wrong..."));
            },
            complete: function (jqXHR, status) {
                oMycontentLoader.hideLoader(oMycontentLoader.LastAccessedContainerId);
            }
        });
    });

    function findSpecificProducts() {
        oMycontentLoader.showLoader("list_info", 12, true);
        var oItemdetail = new ProductDetails();
        oItemdetail.nCid = $("#drpCategory").val();
        oItemdetail.nSCid = $("#drpSubCategory").val();
        oItemdetail.sTitle = $("#txtSearch").val();

        $("#list_info").find('tr').remove();
        $.ajax(approotpath + "Helper/GetProductDetailsJSON", {
            data: oItemdetail,
            type: "POST",
            dataType: 'json',
            success: function (data) {
                var dataJson = $.parseJSON(data.msgJson);
                //$("#list_info").find('tr:gt(0)').remove();
                $("#list_info").find('tr').remove();
                makeItemGrid(dataJson);
                //self.categories(mappedCategories);
            },
            error: function (jqXHR, status, err) {
                console.log("Error raised: " + err);
                $("#list_info").append(oMycontentLoader.noRecordExist(12, "Oop's Something went wrong..."));
            },
            complete: function (jqXHR, status) {
                oMycontentLoader.hideLoader(oMycontentLoader.LastAccessedContainerId);
            }
        });
    }
    //makeVirusGrid():: Lists virus details
    function makeItemGrid(itemGridList) {
        if (itemGridList.length > 0) {
            itemGridListGlobal.push(itemGridList);
            if (document.getElementById("list_info")) {
                var info_template = $("#tpl_listinfo").html();
                var vcount = 0;
                var vdesc = "";
                for (vcount = 0; vcount < itemGridList.length; vcount++) {
                    vdesc = htmlDecode(itemGridList[vcount].sItemDescription); 
                    $("#list_info").append(
                        info_template.replace(/{srno}/g, (vcount + 1))
                        
                        .replace(/{itemid}/g, itemGridList[vcount].nPid)
                        .replace(/{pname}/g, itemGridList[vcount].sTitle)
                        //.replace(/{plink}/g, itemGridList[vcount].sItemName)
                        .replace(/{pcname}/g, itemGridList[vcount].sCatName)
                        .replace(/{pscname}/g, itemGridList[vcount].sSubCatName)
                        .replace(/{phlnk}/g, itemGridList[vcount].sHomelnk)
                        .replace(/{pdlnk}/g, itemGridList[vcount].sDownloadlnk)
                        .replace(/{pblnk}/g, itemGridList[vcount].sBuyNowlnk)
                        .replace(/{cname}/g, itemGridList[vcount].sCompanyName)
                        .replace(/{cur}/g, itemGridList[vcount].sCurSymbol)
                        .replace(/{pslug}/g, itemGridList[vcount].sSlug)
                        .replace(/{bprice}/g, itemGridList[vcount].dBasePrice)
                        .replace(/{nprice}/g, itemGridList[vcount].dNetPrice)
                        .replace(/{disc}/g, itemGridList[vcount].nDiscountOffered)
                     );
                }

            }
        }
        var ignoreRows = 0;
        if (document.getElementById("list_info" + "_loader")) {
            ignoreRows = 1;
        }
        if ($("#list_info").find("tr").length <= ignoreRows) {
            $("#list_info").append(oMycontentLoader.noRecordExist(12, "No More Records Exists"));
        }
    }
    
</script>

